package com.egao.common.module.cms.controller;

import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.cms.entity.Article;
import com.egao.common.module.cms.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 文章管理
 *
 * @author pyx
 */
@Api(tags = "文章管理")
@RestController
@RequestMapping("${api}")
public class ArticleController extends BaseController {

    @Autowired
    private ArticleService articleService;

    @PreAuthorize("hasAuthority('cms:article:list')")
    @ApiOperation("分页查询文章")
    @OperLog(value = "文章管理", desc = "分页查询文章")
    @GetMapping("/cms/article/page")
    public PageResult<Article> page(HttpServletRequest request) {
        PageParam<Article> pageParam = new PageParam<>(request);
        pageParam.setDefaultOrder(null, new String[]{"ca.create_time"});
        return new PageResult<>(articleService.listPage(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('cms:article:insert')")
    @ApiOperation("添加文章")
    @OperLog(value = "文章管理", desc = "添加文章", result = true)
    @PostMapping("/cms/article/insert")
    public JsonResult insert(@RequestBody Article article) {
        return articleService.insertArticle(article);
    }

    @PreAuthorize("hasAuthority('cms:article:update')")
    @ApiOperation("修改文章")
    @OperLog(value = "文章管理", desc = "修改文章", result = true)
    @PutMapping("/cms/article/update")
    public JsonResult update(@RequestBody Article article) {
        return articleService.updateArticle(article);
    }

    @PreAuthorize("hasAuthority('cms:article:delete')")
    @ApiOperation("删除文章")
    @OperLog(value = "文章管理", desc = "删除文章", result = true)
    @DeleteMapping("/cms/article/delete")
    public JsonResult delete(Integer id) {
        return articleService.deleteArticle(id);
    }

    @PreAuthorize("hasAuthority('cms:article:detail')")
    @ApiOperation("查询单个文章")
    @OperLog(value = "文章管理", desc = "查询单个文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文章id", required = false, dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/cms/article/get")
    public JsonResult listGetId(Integer id) {
        return articleService.getId(id);
    }

    @PreAuthorize("hasAuthority('cms:article:list')")
    @ApiOperation("查询所属栏目下置顶文章个数")
    @OperLog(value = "文章管理", desc = "查询栏目下置顶文章数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "所属栏目id", required = false, dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/cms/article/top")
    public JsonResult topFlagNum(Integer categoryId) {
        return JsonResult.ok().setData(articleService.topFlagNum(categoryId).size());
    }
}
